﻿Public Class ActivityLogger
    Private llogsetting As ActivityLoggerConf
    Private logfile As System.IO.StreamWriter
    Private currentlogfile As String
    Private seq As Long
    Private streaming As Boolean
    Private Shared instance As ActivityLogger


    Private Sub New()
        streaming = False
        logconfig = New ActivityLoggerConf
    End Sub

    Public Property logconfig As ActivityLoggerConf
        Get
            Return llogsetting
        End Get
        Set(ByVal value As ActivityLoggerConf)
            llogsetting = value
            checkForLog()
        End Set
    End Property



    Private Function createLogfile() As String
        Dim cm As String = ""
        If (logconfig.rotatelog) Then
            cm = "ewinsync_" + commons.dateToString(Today) + ".log"
        Else
            cm = "ewinsync.log"
        End If
        If (String.IsNullOrEmpty(logconfig.logfolder.Trim)) Then
            logconfig.logfolder = My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString
        End If
        cm = logconfig.logfolder + "\" + cm
        Return cm
    End Function
    Public Function log(ByVal lmod As String, ByVal lg As String, ByVal severity As String) As Boolean
        Dim cm As String = seq.ToString + "," + commons.todayAsString
        cm = cm + "," + lmod + "," + lg + "," + severity
        Try
            logfile.WriteLine(cm)
            logfile.Flush()
            seq = seq + 1
        Catch ex As Exception
            Return False
        End Try
        
        Return True
    End Function
    Private Function checkForLog() As Boolean
        Dim filename = createLogfile()
        If (filename <> currentlogfile) Then
            'close the previous
            closeLog()
        Else
            If (streaming) Then Return True
        End If
        currentlogfile = filename
        openLog(currentlogfile)
        logLogOpening()
        Return True
    End Function
    Public Sub closeLog()
        If (IsNothing(logfile)) Then Return
        Try
            logfile.Flush()
            logfile.Close()
        Catch ex As Exception
            log(commons.MOD_LOGGER, ex.Message, ActivityLoggerConf.LOGLEVEL_ERROR)
        End Try
       
    End Sub
    Private Sub logLogOpening()
        log(commons.MOD_LOGGER, "Loggin started", ActivityLoggerConf.LOGLEVEL_INFO)
    End Sub
    Public Sub openLog(ByVal file As String)
        Try
            logfile = New System.IO.StreamWriter(file, True)
            streaming = True
            seq = 1
        Catch ex As Exception
            MsgBox(ex.Message, vbExclamation)
        End Try
    End Sub
   

    Public Function getCurrentLog() As String
        Return currentlogfile
    End Function

    Public Shared Function getInstance() As ActivityLogger
        If (IsNothing(instance)) Then instance = New ActivityLogger
        Return instance
    End Function
End Class
